﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace SoundManager
{
    public class Grid : Microsoft.Xna.Framework.DrawableGameComponent
    {

        #region Fields

        // Our grid size vertically (NOTE: Must be an even number since the lines come in pairs of vertices)
        const int VERTICAL_SIZE = 20;

        // Our grid size horizontally (NOTE: Must be an even number since the lines come in pairs of vertices)
        const int HORIZONTAL_SIZE = 26;

        // Horizontal line length (absolute value starting from X-coordinate 0)
        const int HORIZONTAL_LINE_LENGTH = 15;

        // Vertical line length (absolute value starting from Z-coordinate 0)
        const float VERTICAL_LINE_LENGTH = 22.5f;

        // Horizontal and vertical limits before looping to the other side of the grid
        public float LIMIT_LEFT = -14.0f;
        public float LIMIT_RIGHT = 14.0f;
        public float LIMIT_TOP = -10.0f;
        public float LIMIT_BOTTOM = 10.0f;

        // Where we set up our drawing properties (world, view, projection, shaders, etc)
        BasicEffect basicEffect;

        // An array of points for our horizontal lines
        VertexPositionColor[] horzVertices;

        // An array of points for our vertical lines
        VertexPositionColor[] verticalVertices;

        // Graphics device manager
        GraphicsDevice graphics;

        #endregion

        public Grid(Game game)
            : base(game)
        {
            graphics = game.GraphicsDevice;
        }


        public void Initialize(Matrix world, Matrix view, Matrix projection)
        {
            basicEffect = new BasicEffect(graphics);
            basicEffect.VertexColorEnabled = true;

            basicEffect.World = world;

            basicEffect.View = view;

            basicEffect.Projection = projection;

            horzVertices = new VertexPositionColor[VERTICAL_SIZE];
            verticalVertices = new VertexPositionColor[HORIZONTAL_SIZE];
            float spacer = 0.0f;

            // Horizontal lines
            for (int i = 0; i < VERTICAL_SIZE; ++i)
            {
                if (i % 2 == 0)
                {
                    spacer = (i * 2.5f) / 2;
                    horzVertices[i].Position = new Vector3(-HORIZONTAL_LINE_LENGTH, 0, -spacer);
                }
                else
                    horzVertices[i].Position = new Vector3(HORIZONTAL_LINE_LENGTH, 0, -spacer);

                horzVertices[i].Color = Color.Black;
            }

            // Reset spacer
            spacer = 0f;

            // Vertical lines
            // For some reason our X-coordinates go from +X to -X, rather than from -X to +X (when reading left to right)
            for (int i = 0; i < HORIZONTAL_SIZE; ++i)
            {
                if (i % 2 == 0)
                {
                    spacer = (i * 2.5f) / 2;
                    verticalVertices[i].Position = new Vector3(-HORIZONTAL_LINE_LENGTH + spacer, 0, 0);
                }
                else
                    verticalVertices[i].Position = new Vector3(-HORIZONTAL_LINE_LENGTH + spacer, 0, -VERTICAL_LINE_LENGTH);

                verticalVertices[i].Color = Color.Black;
            }

        }



        public void Draw(GameTime gameTime)
        {
            basicEffect.CurrentTechnique.Passes[0].Apply();
            graphics.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, horzVertices, 0, VERTICAL_SIZE / 2);
            graphics.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, verticalVertices, 0, HORIZONTAL_SIZE / 2);
        }



    }
}